package com.yuanian.ecsplus.meta;

import com.google.common.collect.Maps;
import com.yuanian.businessobject.model.vo.base.BOSaveOrUpdateParam;
import com.yuanian.ecsplus.platform.PlatformSupport;
import com.yuanian.ecsplus.platform.SpringContextBean;
import com.yuanian.ecsplus.platform.TestPlatformSupport;
import com.yuanian.ecsplus.reflect.PersistenceMetaClass;
import com.yuanian.ecsplus.reflect.SimplePersistenceMetaClass;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 抽象元数据
 * @param <T>
 */
public abstract class BaseMeta<T> implements Meta<T> {

    //持久化元字符
    protected PersistenceMetaClass<T> persistenceMetaClass;

    //平台的支撑类
    protected PlatformSupport platformSupport;

    //业务对象对应平台的apiName
    protected String apiName;

    public BaseMeta(Class<T> clz) {
        //this.platformSupport = new TestPlatformSupport();
        this.platformSupport = SpringContextBean.getBean(PlatformSupport.class);
        this.persistenceMetaClass = new SimplePersistenceMetaClass<>(clz);
        this.apiName = this.getMetaClass().getApiName();
    }

    public PlatformSupport getPlatformSupport() {
        return this.platformSupport;
    }

    @Override
    public PersistenceMetaClass<T> getMetaClass() {
        return this.persistenceMetaClass;
    }

    @Override
    public int batchSave(List<T> list) {
        //平台批量保存需要的map
        List<Map> mapData = new ArrayList<>();
        for (T t : list) {
            //对象转map
            mapData.add(this.getMetaClass().toMap(t));
        }

        Map logicMap = Maps.newHashMap();
        logicMap.put(getApiName(), this.getMetaClass().getIdField().getField().getName());

        BOSaveOrUpdateParam param = new BOSaveOrUpdateParam();
        param.setExcuteCheckRule(false);
        //调用平台接口
        this.platformSupport.saveOrUpdate(getApiName(), null, mapData, logicMap, param);
        return list.size();
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    public int batchDelete(List<T> list) {
        //平台批量保存需要的map
        List<String> idList = new ArrayList<>();
        for (T t : list) {
            idList.add(getObjectId(t));
        }
        return this.platformSupport.batchDeleteBoDataById(getApiName(), null, idList);
    }

    @Override
    public void save(T obj) {
        batchSave(Arrays.asList(obj));
    }

    @Override
    public void insert(T obj) {
        Map map = platformSupport.insertBoData(getApiName(), null, this.getMetaClass().toMap(obj));
        //插入后反写obj,补全字段
        platformSupport.boMapFullDataConvert(map, apiName);
        this.getMetaClass().copyProperty(obj, map);
    }


    public void issue(T obj) {
        platformSupport.issueBoData(getApiName(), null, this.getMetaClass().toMap(obj));
    }

    public void update(T obj) {
        platformSupport.updateBoData(getApiName(), null, getObjectId(obj), this.getMetaClass().toMap(obj));
    }

    public void delete(T obj) {
        platformSupport.deleteByBoMainId(getApiName(), null, getObjectId(obj));
    }

    /**
     * 获取对象的objectId
     * @param obj
     * @return
     */
    abstract protected String getObjectId(T obj);

    public String getApiName() {
        return apiName;
    }
}
